using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using Entities;

namespace DAL
{
    public class tbBinhLuanDAL
    {
        private SqlConnection cn;
        public tbBinhLuanDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }
        public tbBinhLuanDAL(tbBinhLuanDAL binhluan)
        {
            this.cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            this.cn.Open();
        }
        public void Insert(tbBinhLuan binhluan)
        {
            SqlCommand cm = new SqlCommand("sp_tbBinhLuan_insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@KhachHang", binhluan.KhachHang);
            cm.Parameters.AddWithValue("@SanPham", binhluan.SanPham);
            cm.Parameters.AddWithValue("@NoiDung", binhluan.NoiDung);
            cm.Parameters.AddWithValue("@NgayGui", binhluan.NgayGui);
            cm.ExecuteNonQuery();
        }
        public void Update(tbBinhLuan binhluan)
        {
            SqlCommand cm = new SqlCommand("sp_tbBinhLuan_update", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaBL", binhluan.MaBL);
            cm.Parameters.AddWithValue("@KhachHang", binhluan.KhachHang);
            cm.Parameters.AddWithValue("@SanPham", binhluan.SanPham);
            cm.Parameters.AddWithValue("@NoiDung", binhluan.NoiDung);
            cm.Parameters.AddWithValue("@NgayGui", binhluan.NgayGui);
            cm.ExecuteNonQuery();
        }
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("sp_tbBinhLuan_delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaBL", iD);
            cm.ExecuteNonQuery();
        }
        public tbBinhLuanCollection Get(string where, int pageIndex, int pageSize,
            string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("sp_tbBinhLuan_get", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;

            SqlDataReader dr = cm.ExecuteReader();
            tbBinhLuanCollection blCollection = new tbBinhLuanCollection();

            while (dr.Read())
            {
                tbBinhLuan binhluan = new tbBinhLuan();
                binhluan.MaBL = int.Parse(dr.GetValue(0).ToString());
                binhluan.KhachHang = int.Parse(dr.GetValue(1).ToString());
                binhluan.SanPham = int.Parse(dr.GetValue(2).ToString());
                binhluan.NoiDung = dr.GetValue(3).ToString();
                binhluan.NgayGui =DateTime . Parse ( dr.GetValue(4).ToString());
                blCollection.Add(binhluan);
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return blCollection;
        }
    }
}